using UnityEngine;

/// <summary>
/// PoolManager 辅助类 - 确保 PoolManager 始终可用
/// </summary>
public static class PoolManagerHelper
{
    /// <summary>
    /// 获取 PoolManager 实例，如果不存在则自动创建
    /// </summary>
    /// <returns>PoolManager 实例</returns>
    public static PoolManager GetOrCreatePoolManager()
    {
        if (PoolManager.Instance != null)
        {
            return PoolManager.Instance;
        }

        Logger.LogWarning("PoolManagerHelper", "PoolManager.Instance 为 null，正在自动创建...");
        
        // 创建 PoolManager GameObject
        GameObject poolManagerGO = new GameObject("PoolManager");
        var poolManager = poolManagerGO.AddComponent<PoolManager>();
        
        // 使用 Object.DontDestroyOnLoad 而不是 DontDestroyOnLoad
        Object.DontDestroyOnLoad(poolManagerGO);
        
        Logger.Log("PoolManagerHelper", "PoolManager 自动创建成功");
        return poolManager;
    }

    /// <summary>
    /// 安全地获取 PoolManager 实例
    /// </summary>
    /// <returns>PoolManager 实例，如果创建失败则返回 null</returns>
    public static PoolManager SafeGetPoolManager()
    {
        try
        {
            return GetOrCreatePoolManager();
        }
        catch (System.Exception e)
        {
            Logger.LogError("PoolManagerHelper", $"获取 PoolManager 时发生错误: {e.Message}");
            return null;
        }
    }
}
